package com.pj.wisdomlogistics.ui.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.pj.spinnerlib.views.DensityUtil;
import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.MeetingSimpleInfo;
import com.pj.wisdomlogistics.mvp.contract.MeetingChooseContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.MeetingChoosePresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.MeetingSimpleAdapter;
import com.pj.wisdomlogistics.ui.view.GridSpacingItemDecoration;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.CustomToast;
import com.pj.wisdomlogistics.utils.ScreenUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MeetingChooseActivity extends MvpActivity<MeetingChoosePresenter> implements MeetingChooseContract.View, MeetingSimpleAdapter.onItemClick {
    @BindView(R.id.rv_choose_meeting)
    RecyclerView mRvChooseMeeting;
//    @BindView(R.id.tv_select_date)
//    TextView mTvDate;
    @BindView(R.id.tv_title)
    TextView mTvTitle;

    private MeetingSimpleAdapter mAdapter;
    private View contentView;
    private int screenWidth;
    private int screenHeight;

    private boolean isFirst;//第一次选择会议室，跳出咨询提示


    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initView() {
        ultimateBar.setColorBar(ContextCompat.getColor(this , R.color.text_blue));

        //mTvDate.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis())));

        isFirst = true;

        mRvChooseMeeting.setLayoutManager(new GridLayoutManager(this , 2 , LinearLayoutManager.VERTICAL , false));
        mRvChooseMeeting.addItemDecoration(new GridSpacingItemDecoration(2 , DensityUtil.dip2px(this , 20) , false));
        mAdapter = new MeetingSimpleAdapter(this);
        mRvChooseMeeting.setAdapter(mAdapter);
        mAdapter.setListener(this);

        mPresenter.findMeetingRoomList();
        showProgressDialog();
    }



    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_choose_meeting;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }


    @OnClick({R.id.iv_back , R.id.tv_select_date})
    public void onViewClicked(View mView) {
        switch (mView.getId()){
            case R.id.iv_back:
                ActivityManagerUtil.create().finishActivity();
                break;
            case R.id.tv_select_date:
                //CalendarUtil.create().setCalendarDate(this , mTvDate);
                break;
        }
    }

    @Override
    public void itemClick(int id , MeetingSimpleInfo mInfo) {
        if(isFirst){
            showTurnDown("02587778188");
            isFirst = false;
        }else{

            Intent mIntent = new Intent(this, MeetingPlanActivity.class);
            mIntent.putExtra(Cons.MeetingInfo.MEETING_SELECT_ID , id);
            mIntent.putExtra(Cons.MeetingInfo.MEETING_COUNT , mInfo);
            startActivity(mIntent);
        }

    }

    public void showTurnDown(final String text){
        screenWidth = ScreenUtils.getScreenWidth(this);
        screenHeight = ScreenUtils.getScreenHeight(this);
        contentView = LayoutInflater.from(this).inflate(R.layout.pop_contact, null);
        final PopupWindow mPopWindow = new PopupWindow(contentView,
                (int) (screenWidth * 0.9), LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        mPopWindow.setTouchable(true);
        mPopWindow.setBackgroundDrawable(new ColorDrawable());
        backgroundAlpha(0.3f);
        Button mBtSend = (Button) contentView.findViewById(R.id.bt_send);
        TextView mTextView = (TextView) contentView.findViewById(R.id.tv_phone);
        mTextView.setText(text);
        mBtSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //拨打电话
                mPopWindow.dismiss();
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + text));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
        mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });

        mPopWindow.showAtLocation(mTvTitle , Gravity.NO_GRAVITY , (int) (screenWidth * 0.05), (int) (screenHeight * 0.3));
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        this.getWindow().setAttributes(lp);
    }

    @Override
    public void showError(String errorMsg) {
        dismissProgressDialog();
        CustomToast.showToast(this ,errorMsg , 1000);
    }

    @Override
    public void showSuccess(List<MeetingSimpleInfo> mList) {
        dismissProgressDialog();
        mAdapter.setList(mList);
    }
}
